package com.baizan.daka.util

import android.text.SpannableString
import android.text.Spanned
import android.text.style.*
import java.util.*

class HyperTextUtil private constructor() {

    class build {
        var mSpannableString: SpannableString? = null
        var sb = StringBuffer()
      private  var list: MutableList<Bean> = ArrayList()

        constructor(text: String?) {
            sb.append(text)
        }

        constructor() {}

        fun Builder(): SpannableString {
            mSpannableString = SpannableString(sb)
            for (b in list) {
                mSpannableString?.setSpan(b.span, b.start, b.end, b.flag)
            }
            return mSpannableString!!
        }

        /**
         * @param text
         * @param color 颜色
         * @return
         */
        fun appendColor(text: String, color: Int): build {
            add(text, ForegroundColorSpan(color))
            return this
        }

        /**
         * @param text
         * @param size 大小
         * @return
         */
        fun appendSize(text: String, size: Int): build {
            add(text, AbsoluteSizeSpan(AppUtils.dp2px(size)))
            return this
        }

        fun appendText(text: String): build {
            add(text)
            return this
        }

        /**
         * @param text
         * @param type Typeface.BOLD.
         * @return
         */
        fun appendStyle(text: String, type: Int): build {
            add(text, StyleSpan(type))
            return this
        }

        fun appendObject(text: String, type: Any): build {
            add(text, type)
            return this
        }

        fun appendObjects(text: String,  type: Any): build {
            add(text, type)
            return this
        }

        /**
         * 设置删除线
         * @param text
         * @param color
         * @return
         */
        fun appendStrikethroughAndColor(text: String, color: Int): build {
            add(text, StrikethroughSpan(), ForegroundColorSpan(color))
            return this
        }

        /**
         * 设置下划线
         * @param text
         * @param clickableSpan 点击事件
         * @return
         */
        fun appendUnderlineAndColor(
            text: String,
            clickableSpan: ClickableSpan?,
            color: Int
        ): build {
            add(text, UnderlineSpan(), clickableSpan!!, ForegroundColorSpan(color))
            return this
        }

        /**
         * @param text
         * @param clickableSpan 点击事件
         * @return
         */
        fun appendUnderline(
            text: String,
            clickableSpan: ClickableSpan?
        ): build {
            add(text, UnderlineSpan(), clickableSpan!!)
            return this
        }

        /**
         * @param text
         * @param type Typeface.BOLD.
         * @return
         */
        fun appendStyleAndSize(text: String, size: Int, type: Int): build {
            add(text, StyleSpan(type), AbsoluteSizeSpan(AppUtils.dp2px(size)))
            return this
        }

        /**
         * @param text
         * @param type Typeface.BOLD.
         * @return
         */
        fun appendStyleAndSize(
            text: String,
            size: Int,
            type: Int,
            color: Int
        ): build {
            add(
                text,
                StyleSpan(type),
                AbsoluteSizeSpan(AppUtils.dp2px(size)),
                ForegroundColorSpan(color)
            )
            return this
        }

        private fun add(text: String) {
            sb.append(text)
        }

        private fun add(text: String, vararg span: Any) {
            sb.append(text)
            val start = sb.lastIndexOf(text)
            //setSpan时需要指定的 flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括).
            for (i in 0 until span.size) {
                val bean = Bean(
                    span[i],
                    start,
                    start + text.length,
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE
                )
                list.add(bean)
            }
        }

        private inner class Bean(
             val span: Any,
             val start: Int,
             val end: Int,
             val flag: Int
        )
    }
}
